package com.dearbinge.opentapi.content;

import com.dearbinge.data.api.content.SendContentService;
import com.dearbinge.data.pojo.content.SendContent;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurerAdapter;

import java.util.ArrayList;
import java.util.List;


@Api(description = "发送信息")
@RestController
@RequestMapping("/demoController")
@EnableAutoConfiguration
public class SendContentController  extends WebMvcConfigurerAdapter {

    @Autowired
    private SendContentService sendContentService;

    /**
     * 新增/修改（id为0或者为null：添加/id不为空或者不为0：修改）
     */
    @ApiOperation(value = "添加发送数据/修改发送数据",notes = "添加发送数据/修改发送数据")
    @RequestMapping(value = "/addOrUpdate",method =  RequestMethod.POST)
    public String addOrUpdate(@RequestBody SendContent sendContent){
        String dataStr = null;
        try{
            Integer dataCode = 0;
            if (sendContent.getFeedId() != null && sendContent.getFeedId() != 0){
                dataCode = sendContentService.update(sendContent);
                if (dataCode > 0){
                    dataStr = "修改成功！";
                }
            }else {
                dataCode = sendContentService.insert(sendContent);
                if (dataCode > 0){
                    dataStr = "添加成功！";
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        System.out.print(dataStr);
            return dataStr;
    }

    /**
     *查询全部
     */
    @ApiOperation(value = "查询全部",notes = "查询全部")
    @RequestMapping(value = "/getAll",method = RequestMethod.GET)
    public List<SendContent> getAll(){
        List<SendContent> sendContent = new ArrayList<>();
        try{
            sendContent =  sendContentService.getAll();
            System.out.print(sendContent);
            return sendContent;
        }catch (Exception e){
            e.printStackTrace();
        }
        return sendContent;
    }

    /**
     *统计
     */
    @ApiOperation(value = "统计数量",notes = "统计数量")
    @RequestMapping(value = "/getCount",method = RequestMethod.GET)
    public String getCount(){
        Integer count = 0;
        try{
            count = sendContentService.getCount();
        }catch (Exception e){
            e.printStackTrace();
        }
        return count.toString();
    }

    /**
     * 根据ID查询数据
     */
    @ApiOperation(value = "根据ID查询",notes = "根据ID查询")
    @ApiImplicitParam(name = "id",value = "发送内容ID",paramType="query")
    @RequestMapping(value = "/getById",method = RequestMethod.GET)
    public SendContent getById(String id){
        SendContent sendContent = new SendContent();
        try{
            Integer integer = new Integer(id);
            sendContent = sendContentService.getById(integer);
        }catch (Exception e){
            e.printStackTrace();
        }
        return sendContent;
    }

}
